/* Autostop
 *
 * Copyright (c) Maciej Gajewski, Grzegorz Latarowski
 * http://code.google.com/p/autostop-automaty-drogowe
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
 */

#ifndef IMAGEANALYZER_H
#define IMAGEANALYZER_H

// qt
#include <QThread>
#include <QVariantMap>

//gparams
#include "gparams.h"

// gnet
#include "gnetudp.h"
class GAutoPacket;

// analyzer
#include "analyzer.h"

// local
#include "camera1params.h"

/// This is main analyzer class. It intilaizes camera, analyzer process and SQL connection,
/// acquires images and anayzes it reporting intersting results into database.
/// Use the 'run' method to start it. It will return only on clean exit of sime kind,
/// throw exception otherwise.
/// This class inherits QThread and may run as a separate thread
class ImageAnalyzer : public QThread
{
	Q_OBJECT
public:
	
	// construction / destruction
	ImageAnalyzer();
	virtual ~ImageAnalyzer(){}
	
	/// Thread entry
	virtual void run();

protected:

	
	/// Intializes analyzer
	void init();
	
	/// loads settings from file
	void loadSettings();
	
	/// Loads ROI and martkers from files
	void loadMasks();
		
	/// Reports object to database
	void reportObject( const object_t& object );
		
	/// Reports object to adjuster
	void reportObjectToAdjuster( const object_t& object, bool speeding  );
	
	/// Reads process parameters
	void readParams( const GParams& params );
	
	/// Fill image with its own difference from process backgorund
	void prepareDiff( pimage_t pImage );
	/// Grays all pixels outside of ROI
	void addRoi( pimage_t pImage );
	
	/// Sends JPEG via UDP
	void sendJpeg( const QHostAddress& address, quint16 port );
	
	/// Sends objects via UDP
	void sendObjects( const QHostAddress& address, quint16 port ); 
	
	/// Sends image via udp
	void sendImage( const QHostAddress& address, quint16 port, pimage_t pImage );
	
	/// Sends GAutoPacket via UDP
	void sendUdpPacket( const GAutoPacket& packet, const QHostAddress& address, quint16 port );
	
	/// Check for interesting objects in analyzer output
	void checkForInterestingObjects();
	
	/// Initializes GPIO alarm 
	void initHwAlarm();
	
	/// Turns on HW alarm by clearing GPIO line
	void turnOnHwAlarm();
	
	/// Truns off HW alarm by setting GPIO line
	void turnOffHwAlarm();
	
	// data
	pprocess_t _pProcess;		///< Analyzer process
	
	
	// TODO remove unused params
	bool	_showDiff;			///< If backgorund/foreground differemnce should be put into image
	bool	_showRoi;			///< If ROI shpould be shown on image
	
	int 	_intervalJpeg;		///< Jpeg interval
	double 	_max_speed;			///< Max allowed speed [m/s]
	int		_image_format;		///< Requested image format
	quint32	_image_id;			///< Current id image
	bool	_showAllObjects;	///< Show also invalid objects
	QHostAddress _secondCameraAddr;///< 2nd camera IP
	int		_objectNumber;		///< Object counter

	bool	_wasAccelExceeded; 	///< accel exceed flag
	bool	_hwAlarmOn;			///< Flag: is hardware alarm ON?
	double _hwAlarmTurnOnTime;	///< Time when alarm was turned on
	
	// settings
	double _accelExpectedX;		///< Expected value of x accell
	double _accelExpectedY;		///< Expected value of y accell
	double _accelToleranceX;	///< Tolerated margin around expected value
	double _accelToleranceY;	///< Tolerated margin around expected value
	double _accelMaxChangeX;	///< Accepted tolerance change 
	double _accelMaxChangeY;	///< Accepted tolerance change
	
	
	GUdpSender	_udpSender;		///< Udp sender tool

};

#endif // IMAGEANALYZER_H

// eof


